package com.yzdmw.toutiao;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import com.yzdmw.toutiao.information_edit.edit_page;
import com.yzdmw.toutiao.login.LoginActivity;
import com.yzdmw.toutiao.user.UserSignedViewModel;

public class PersonalPageActivity extends AppCompatActivity {

    TextView toutiao_tv, likes_tv, fans_tv, follows_tv, edit_information, nickname, introduct;
    int toutiao_num=100, likes_num=30, fans_num=666, follows_num=2333;
    String str1, str2, str3, str4;
    ImageView myRD;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.personalpage);

        str1 = Integer.toString(toutiao_num) + "头条";
        str2 = Integer.toString(likes_num) + "获赞";
        str3 = Integer.toString(fans_num) + "粉丝";
        str4 = Integer.toString(follows_num) + "关注";

        toutiao_tv = (TextView)findViewById(R.id.toutiao);
        likes_tv = (TextView)findViewById(R.id.likes);
        fans_tv = (TextView)findViewById(R.id.fans);
        follows_tv = (TextView)findViewById(R.id.follows);
        edit_information = (TextView)findViewById(R.id.edit_information);
        nickname = (TextView)findViewById(R.id.nickname);
        introduct = (TextView)findViewById(R.id.introduction);
        myRD = (ImageView)findViewById(R.id.head_portrait);

        toutiao_tv.setText(str1);
        likes_tv.setText(str2);
        fans_tv.setText(str3);
        follows_tv.setText(str4);
        edit_information.setOnClickListener(view -> {
            Intent intent = new Intent(this, edit_page.class);
            startActivityForResult(intent,13);
        });
        String name = getIntent().getStringExtra("name");
        if(name != null && !name.equals("")) {
            nickname.setText(name);
        }
        byte[] byteArray = getIntent().getByteArrayExtra("profile");
        if(byteArray != null && byteArray.length != 0) {
            Bitmap profile = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
            myRD.setImageBitmap(profile);
        }
        getSupportActionBar().hide();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d("USER","requestCode:"+requestCode+",resultCode:"+resultCode);
        if (requestCode == 13 && resultCode == 23) {
            String name = data.getStringExtra("name");
            String introduction = data.getStringExtra("introduction");
            String birthday = data.getStringExtra("birthday");
            String local = data.getStringExtra("local");
            int gen = data.getIntExtra("gen", 0);
            byte[] byteArray = data.getByteArrayExtra("profile");
            Intent intent = new Intent();
            if(byteArray != null && byteArray.length != 0) {
                Bitmap profile = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
                myRD.setImageBitmap(profile);
                intent.putExtra("profile", byteArray);
            }
            else {
                intent.putExtra("profile", "");
            }
            if(name != null && !name.equals("")) {
                nickname.setText(name);
                intent.putExtra("name", nickname.getText().toString());
            }
            else {
                intent.putExtra("name", "");
            }
            setResult(24,intent);
            if(introduction != null && !introduction.equals("")) {
                introduct.setText(introduction);
            }
        }

    }

}